import qbs
import qbs.TextFile
import qbs.FileInfo

import "private.js" as Private

/**
  Project directories. This module acts as central configuration point and lookup facility for project directories. It can also
  generate 'cutehmi.dirs.hpp' artifact for a product, which contains macros describing directory locations.
  */
Module {
	additionalProductTypes: ["cutehmi.dirs"]

	setupRunEnvironment: {
		Private.setupEnvironment()
	}

	setupBuildEnvironment: {
		Private.setupEnvironment()
	}

	PropertyOptions {
		name: "artifacts"
		description: "Whether to generate any artifacts."
	}
	property bool artifacts: false

	PropertyOptions {
		name: "dirsHppArtifact"
		description: "Actual filename of artifact tagged as 'cutehmi.dirs.hpp'. If set to undefined artifact won't be generated."
	}
	property path dirsHppArtifact: artifacts ? product.cutehmi.conventions.functions.autogenIncludesSubdir(product.name) + "/cutehmi.dirs.hpp"
											 : undefined

	PropertyOptions {
		name: "installDir"
		description: "Project instalation directory composed of 'qbs' properties describing instalation path."
	}
	readonly property string installDir: product.qbs.installPrefix ? product.qbs.installRoot + product.qbs.installPrefix : product.qbs.installRoot // Note: qbs.installPrefix starts with "/".

	PropertyOptions {
		name: "extensionsInstallSubdir"
		description: "Target intallation subdirectory for extensions."
	}
	// <qbs-cutehmi.dirs-1.workaround target="Qt" cause="design">
	// Android expects QML files to be installed in 'qml' directory, so we're changing installation path of extension files.
	property string extensionsInstallSubdir: qbs.targetOS.contains("android") ? "cutehmi/qml" : "cutehmi/app"
	// </qbs-cutehmi.dirs-1.workaround>

	PropertyOptions {
		name: "extensionsSourceDir"
		description: "Directory where source code of extensions reside."
	}
	readonly property string extensionsSourceDir: project.sourceDirectory + "/extensions"

	PropertyOptions {
		name: "toolsSourceDir"
		description: "Directory where source code of tools resides."
	}
	readonly property string toolsSourceDir: project.sourceDirectory + "/tools"

	PropertyOptions {
		name: "externalDeployDir"
		description: "Deployment directory of external libraries."
	}
	property string externalDeployDir: project.sourceDirectory + "/external/deploy"

	PropertyOptions {
		name: "externalLibDir"
		description: "Directory containing external libraries binaries."
	}
	readonly property string externalLibDir: externalDeployDir + "/lib"

	PropertyOptions {
		name: "externalIncludeDir"
		description: "Directory containing external libraries includes."
	}
	readonly property string externalIncludeDir: externalDeployDir + "/include"

	PropertyOptions {
		name: "puppetSourceSubdir"
		description: "subdirectory containing QML puppet files to be used with Qt Designer."
	}
	readonly property string puppetSourceSubdir: "puppet"

	PropertyOptions {
		name: "puppetsInstallSubdir"
		description: "Installation subdirectory of puppet extensions that are used by Qt Designer."
	}
	property string puppetsInstallSubdir: "cutehmi/puppets"

	PropertyOptions {
		name: "testsInstallSubdir"
		description: "Target intallation subdirectory for tests."
	}
	property string testsInstallSubdir: "cutehmi/app"

	PropertyOptions {
		name: "toolsInstallSubdir"
		description: "Target intallation subdirectory for a tool."
	}
	property string toolsInstallSubdir: "cutehmi/app"

	PropertyOptions {
		name: "translationsInstallSubdir"
		description: "Target intallation subdirectory for translations."
	}
	property string translationsInstallSubdir: "cutehmi/translations"

	PropertyOptions {
		name: "artifactsInstallSubdir"
		description: "Target intallation subdirectory for various artifacts."
	}
	property string artifactsInstallSubdir: "cutehmi/artifacts"

	PropertyOptions {
		name: "metadataInstallSubdir"
		description: "Target intallation subdirectory for metadata."
	}
	property string metadataInstallSubdir: "cutehmi/metadata"

	Depends { name: "cutehmi.conventions" }

	Rule {
		condition: product.cutehmi.dirs.dirsHppArtifact !== undefined
		multiplex: true

		prepare: {
			var hppCmd = new JavaScriptCommand();
			hppCmd.description = "generating " + output.filePath
			hppCmd.highlight = "codegen";
			if (product.dedicatedInstallSubdir !== undefined)
				hppCmd.toolRelativePath = FileInfo.relativePath(product.cutehmi.dirs.installDir + "/" + product.cutehmi.dirs.toolsInstallSubdir, product.cutehmi.dirs.installDir + "/" + product.dedicatedInstallSubdir)
			else
				hppCmd.toolRelativePath = undefined
			hppCmd.projectRelativePath = FileInfo.relativePath(project.sourceDirectory, product.sourceDirectory)
			hppCmd.sourceCode = function() {
				var f = new TextFile(output.filePath, TextFile.WriteOnly);
				try {
					var prefix = "CUTEHMI_DIRS"

					f.writeLine("#ifndef " + prefix + "_HPP")
					f.writeLine("#define " + prefix + "_HPP")

					f.writeLine("")
					f.writeLine("// This file has been autogenerated by 'cutehmi.dirs' Qbs module.")
					f.writeLine("// Warning: don't include this file in public header!")
					f.writeLine("")
					f.writeLine("#define " + prefix + "_TOOLS_INSTALL_SUBDIR \"" + product.cutehmi.dirs.toolsInstallSubdir + "\"")
					f.writeLine("#define " + prefix + "_TESTS_INSTALL_SUBDIR \"" + product.cutehmi.dirs.testsInstallSubdir + "\"")
					f.writeLine("#define " + prefix + "_EXTENSIONS_INSTALL_SUBDIR \"" + product.cutehmi.dirs.extensionsInstallSubdir + "\"")
					f.writeLine("#define " + prefix + "_PUPPETS_INSTALL_SUBDIR \"" + product.cutehmi.dirs.puppetsInstallSubdir + "\"")
					f.writeLine("#define " + prefix + "_TRANSLATIONS_INSTALL_SUBDIR \"" + product.cutehmi.dirs.translationsInstallSubdir + "\"")
					f.writeLine("#define " + prefix + "_METADATA_INSTALL_SUBDIR \"" + product.cutehmi.dirs.metadataInstallSubdir + "\"")
					f.writeLine("#define " + prefix + "_ARTIFACTS_INSTALL_SUBDIR \"" + product.cutehmi.dirs.artifactsInstallSubdir + "\"")
					if (toolRelativePath !== undefined)
						f.writeLine("#define " + prefix + "_TOOL_RELATIVE_PATH" + " \"" + toolRelativePath + "\"	// Relative path between tools installation directory and dedicated installation directory.")
					f.writeLine("#define " + prefix + "_PROJECT_RELATIVE_PATH" + " \"" + projectRelativePath + "\"	// Relative path between project and product source directory.")
					f.writeLine("")
					f.writeLine("#endif")
				} finally {
					f.close()
				}
			}

			return [hppCmd]
		}

		Artifact {
			filePath: product.cutehmi.dirs.dirsHppArtifact
			fileTags: ["cutehmi.dirs.hpp", "cutehmi.dirs", "hpp"]
		}
	}
}

//(c)C: Copyright © 2018-2024, Michał Policht <michal@policht.pl>, Wojtek Zygmuntowicz <wzygmuntowicz.zygmuntowicz@gmail.com>. All rights reserved.
//(c)C: SPDX-License-Identifier: LGPL-3.0-or-later OR MIT
//(c)C: This file is a part of CuteHMI.
//(c)C: CuteHMI is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
//(c)C: CuteHMI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
//(c)C: You should have received a copy of the GNU Lesser General Public License along with CuteHMI.  If not, see <https://www.gnu.org/licenses/>.
//(c)C: Additionally, this file is licensed under terms of MIT license as expressed below.
//(c)C: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//(c)C: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//(c)C: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
